<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        // ['red','blue','red','green','pink','red']，求red出现的位置和次数
        var arr = ['red', 'blue', 'red', 'green', 'pink', 'red'];
        index = arr.indexOf('red');
        var num = 0;
        while (index != -1) {
            console.log('第' + num + '个red的索引号：' + index);
            index = arr.indexOf('red', index + 1);
            num++;
        }
        console.log('red出现的次数：' + num);
    </script>
</head>

<body>

</body>

</html>